<template>
  <div class="sidebar-container van-hairline--right" v-scroll:y.smooth="index">
    <van-skeleton :row="17" :loading="loading">
      <div
        class="nav-item van-hairline--bottom"
        v-for="(item, i) in sideBar"
        :key="i"
        :class="{ active: index == i }"
        @touchend="scrollTo(i)"
        @touchmove="move = true"
        @touchstart="move = false"
      >
        {{ item }}
      </div>
    </van-skeleton>
  </div>
</template>

<script>
import { mapGetters, mapState } from 'vuex';
import scroll from '@/directives/auto_scroll';

export default {
  data() {
    return {
      move: false,
      index: 0,
    };
  },
  directives: {
    scroll,
  },
  computed: {
    ...mapGetters(['sideBar']),
    ...mapState(['loading']),
    type() {
      return this.index === 0
        ? this.sideBar.category
        : this.sideBar[this.index];
    },
  },
  watch: {
    sideBar(value) {
      this.index = 0;
      if (value.length > 0) {
        this.$store.dispatch('fetchGoodsList', {
          type: this.type,
          page: 1,
          sort: 'all',
        });
      } else {
        this.$store.commit('setGoodsList', []);
      }
    },
  },
  methods: {
    scrollTo(i) {
      if (this.move) return;
      this.index = i;
      this.$store.dispatch('fetchGoodsList', {
        type: this.type,
        page: 1,
        sort: 'all',
      });
    },
  },
};
</script>

<style lang="less" scoped>
::v-deep .van-skeleton__row {
  background: #dcdcdc85;
}
.sidebar-container {
  position: fixed;
  left: 0;
  width: 80px;
  top: 135px;
  bottom: 50px;
  overflow: auto;
  background: #f8f8f8;
  .nav-item {
    width: 80px;
    height: 40px;
    line-height: 40px;
    text-align: center;
    position: relative;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
    &.active {
      font-weight: bold;
      color: #ff1a90;
      &::before {
        content: '';
        position: absolute;
        width: 6px;
        height: 18px;
        background: #ff1a90;
        top: 50%;
        left: 0;
        transform: translateY(-50%);
      }
    }
  }
}
.sidebar-container::-webkit-scrollbar {
  width: 0;
  background: none;
}
</style>
